/*
 * @文件描述: 万用表的module
 * @版本: 
 * @作者: 周晨阳
 * @Date: 2021-04-18 15:56:20
 */
#include "multiMeter.h"
#include "VoltageMeter.h"
#include "ResistanceMeter.h"
#include "CurrentMeter.h"
#include "MultiMeterPre.h"
#include "myButton.h"
#include "homePage.h"
/**************函数声明**************/
static unsigned char currentMode;
static uint8_t keyState = 0;
void multiMeterInit();
void onHoldButtonClicked();
void onModeButtonClicked();
void onVoltageButtonClicked();
void onCurrentButtonClicked();
void onResButtonClicked();
void onChangButtonClicked();
/**
 * @brief : 注册万用表按键映射
 * @param {*}
 * @return {*}
 */
void registerMulButton()
{
    //设置按键回调函数
    setF1ButtonListener(onVoltageButtonClicked);
    setF2ButtonListener(onResButtonClicked);
    setF3ButtonListener(onCurrentButtonClicked);
    setF4ButtonListener(onChangButtonClicked);
    setF5ButtonListener(onHoldButtonClicked);
    keyState = 1;
}
/**
 * @brief : 取消万用表按键映射
 * @param {*}
 * @return {*}
 */
void cancelMulButton()
{
    setF1ButtonListener(NULL);
    setF2ButtonListener(NULL);
    setF3ButtonListener(NULL);
    setF4ButtonListener(NULL);
    setF5ButtonListener(NULL);
    keyState = 0;
}
/**
 * @description: 万用表初始化函数
 * @param {type}
 * @return {type}
 */
void multiMeterInit()
{
    //主界面ui初始化
    MultiMeter_GUIInit();
    //不同表初始化
    CurrentMeterInit();
    VoltageMeterInit();
    ResistanceMeterInit();
    if (getModuleState(MODULE_KEYBOARD))
    {
        registerMulButton();
    }
}
/**
 * @brief : 关闭万用表应用
 * @param {*}
 * @return {*}
 */
void closeMultiMeter()
{
    cancelMulButton();
    closeCurrentMeter();
    closeResistanceMeter();
    closeVoltageMeter();
}
/**
 * @description: 数值更新函数，根据当前模式自动开启相应的模块并更新屏幕显示,用一个线程运行
 * @param {type}
 * @return {type}
 */
void MultiMeterValueUpdateLooper()
{
    float value;
    if (getModuleState(MODULE_KEYBOARD) && keyState == 0)
    {
        registerMulButton();
    }

    switch (getCurrentMode())
    {
    case MODE_VOLTAGE:
        //模块更新函数

        VoltageMeterLooper();
        setNumTitle(getVoltage(), getVoltageFactor() == 10 ? 2 : 1);
        setUnit(getU_Unit());
        V_setNiddleAngle(getNiddelAngle());
        break;
    case MODE_CURRENT:
        CurrentMeterLooper();
        setNumTitle(getCurrent(), 2);
        setUnit(getI_Unit());
        V_setNiddleAngle(getCurrentNeedleAngle());

        break;
    case MODE_RESISTANCE:
        ResistanceMeterLooper();
        setNumTitle(getCurrentRes(), getResistanceFactor() == 10 ? 2 : 1);
        setUnit(getR_Unit());
        V_setNiddleAngle(getResNeedleAngle());
        break;
    }
}
/**
 * @description: hold按键按下时暂停屏幕更新功能
 * @param {type}
 * @return {type}
 */
void onHoldButtonClicked()
{
    multiMeterHoldTrigger();
}
/**
 * @description: 模式按键按下时切换模式
 * @param {type}
 * @return {type}
 */
void onModeButtonClicked()
{
}
void onVoltageButtonClicked()
{
    setMeterMode(MODE_VOLTAGE);
}
void onCurrentButtonClicked()
{
    setMeterMode(MODE_CURRENT);
}
void onResButtonClicked()
{
    setMeterMode(MODE_RESISTANCE);
}
void onChangButtonClicked()
{
    setRange(getMeterMode());
}

/**
 * @description: 切换电表模式
 * @param {type}
 * @return {type}
 */
void switchMode(unsigned char mode)
{
    switch (mode)
    {
    case MODE_VOLTAGE:
        currentMode = MODE_VOLTAGE;
        setDisplayMode(MODE_VOLTAGE);

        //打开和关闭相应的模块
        closeCurrentMeter();
        closeResistanceMeter();
        openVoltageMeter();

        break;
    case MODE_CURRENT:
        currentMode = MODE_CURRENT;
        setDisplayMode(MODE_CURRENT);

        closeResistanceMeter();
        closeVoltageMeter();
        openCurrentMeter();

        break;
    case MODE_RESISTANCE:
        currentMode = MODE_RESISTANCE;
        setDisplayMode(MODE_RESISTANCE);

        closeVoltageMeter();
        closeCurrentMeter();
        openResistanceMeter();

        break;
    }
}
/**
 * @description: 取得当前模式
 * @param {type}
 * @return {type}
 */
unsigned char getCurrentMode() { return currentMode; }
